/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.facade.enums;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.enums.Messageable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.beetl.sql.annotation.entity.EnumValue;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(DianlanHardwareFailure)</b>
 * Bit 位表示（0 否 1 是），低位到
 * 高位顺序
 * Bit1：急停按钮动作故障；
 * Bit2：无可用整流模块；
 * Bit3：出风口温度过高；
 * Bit4：交流防雷故障；
 * Bit5：交直流模块 DC20 通信中断；
 * Bit6：绝缘检测模块 FC08 通信中断；
 * Bit7：电度表通信中断；
 * Bit8：读卡器通信中断；
 * Bit9：RC10 通信中断；
 * Bit10：风扇调速板故障；
 * Bit11：直流熔断器故障；
 * Bit12：高压接触器故障；
 * Bit13：门打开；
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/22
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum DianlanHardwareFailure implements Messageable {
    /** 为了对抗 Alibaba Java Coding 而存在的注释 */
    Bit1("01", "急停按钮动作故障"),
    Bit2("02", "无可用整流模块"),
    Bit3("03", "出风口温度过高"),
    Bit4("04", "交流防雷故障"),
    Bit5("05", "交直流模块DC20通信中断"),
    Bit6("06", "绝缘检测模块FC08通信中断"),
    Bit7("07", "电度表通信中断"),
    Bit8("08", "读卡器通信中断"),
    Bit9("09", "RC10通信中断"),
    Bit10("10", "风扇调速板故障"),
    Bit11("11", "直流熔断器故障"),
    Bit12("12", "高压接触器故障"),
    Bit13("13", "门打开"),
    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事");

    /** 枚举值 */
    @EnumValue
    String code;

    /** 枚举描述 */
    String message;


    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return DianlanHardwareFailure
     */
    public static DianlanHardwareFailure getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return DianlanHardwareFailure
     */
    public static DianlanHardwareFailure getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return List<DianlanHardwareFailure>
     */
    public static List<DianlanHardwareFailure> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(DianlanHardwareFailure::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return getByCode(code).message();
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(DianlanHardwareFailure innerEnum) {
        return Optional.ofNullable(innerEnum).map(DianlanHardwareFailure::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(DianlanHardwareFailure::code, DianlanHardwareFailure::message));
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值
     */
    public static <T> T safeGet(DianlanHardwareFailure enumValue, Function<DianlanHardwareFailure, T> mapper, T defaultValue) {
        return Objects.isNull(enumValue) ? defaultValue : mapper.apply(enumValue);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值，默认返回值是null
     */
    public static <T> T safeGet(DianlanHardwareFailure enumValue, Function<DianlanHardwareFailure, T> mapper) {
        return safeGet(enumValue, mapper, null);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的code值，默认返回值是null
     */
    public static String safeGetCode(DianlanHardwareFailure enumValue) {
        return safeGet(enumValue, Messageable::code, null);
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return Boolean
     */
    public boolean isInList(DianlanHardwareFailure... values) {
        for (DianlanHardwareFailure e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }
}